/*
 www.thomasmueller.ch

 Copyright (C) 2004 Thomas Mueller

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

 */

package ch.thomasmueller.saltyfiles.transformer;


import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

import org.bouncycastle.crypto.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

/**
 * This class is used to transform the data form encrypted to decrypted or vice
 * versa. <b>PBEWithMD5AndDES </b> is used as algorithm.
 * 
 * @author Thomas Mueller
 *  
 */
public class TwoFish  extends Transformer
{
	protected int iterationCount = 732;

	public TwoFish(char[] aPassoword, byte[] someSalt)
	{
		super(aPassoword, someSalt);
		Security.addProvider(new BouncyCastleProvider());
		algorithm = "Twofish/CBC/PKCS5Padding";
		
	}
	protected Cipher initCipher(int mode) throws Exception
	{
		
		PBEKeySpec keySpec = new PBEKeySpec(password);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
		SecretKey key = keyFactory.generateSecret(keySpec);
		PBEParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
		Cipher cipher = Cipher.getInstance(algorithm, "BC");

		cipher.init(mode, key, paramSpec);
		return cipher;
		
	}



}